<?php
class gateController extends Zend_Controller_Action
{

    public function indexAction()
    {
        echo("Hi !");
        $this->_helper->viewRenderer->setNoRender(true);
    }

    /**
     * Kiểm tra người dùng
     */
    public function checkuserAction()
    {
        $post = $this->getRequest()->getPost();
        if ($post) {
            $db = Zend_Registry::get('db');
            $tbl_user = new Zend_Db_Table("users");
            $row = $tbl_user->select();
            $row->where("userName=?", $post["username"])
                ->where("userPass=?", md5($post["password"]));
            $user = $tbl_user->fetchRow($row);
            if (!empty($user)) {
                $user['userPass'] = "";
                echo (Zend_Json_Encoder::encode($user->toArray()));
            }

        }
        $this->_helper->viewRenderer->setNoRender(true);
    }

    /**
     *  Lấy tất cả danh mục gốc
     */
    public function gettopcatAction()
    {
        $post = $this->getRequest()->getPost();
        $db = Zend_Registry::get('db');
        $tbl_cat = new Zend_Db_Table('newscats');
        $row = $tbl_cat -> select();
        $row -> from($tbl_cat,array('catName','catSlug'))
        -> where ('catShow=1')
        -> where("catParent=0");
        $row -> order('catOrder');
        $cat = $tbl_cat -> fetchAll($row);
        echo(Zend_Json_Encoder::encode($cat->toArray()));
        $this->_helper->viewRenderer->setNoRender(true);
    }

    /**
     * Lấy tất cả danh mục
     */
    public function getallcatAction()
    {
        $post = $this->getRequest()->getPost();
        $db = Zend_Registry::get('db');
        $tbl_cat = new Zend_Db_Table('newscats');
        $row = $tbl_cat -> select();
        $row -> from($tbl_cat,array('catName','catSlug'))
            -> where ('catShow=1');
        $row -> order('catOrder');
        $cat = $tbl_cat -> fetchAll($row);
        echo(Zend_Json_Encoder::encode($cat->toArray()));
        $this->_helper->viewRenderer->setNoRender(true);
    }

    /**
     *  Tạo thư mục
     */
    public function createdirAction()
    {
        $post = $this->getRequest()->getPost();
        mkdir(UPLOAD_PATH ."/". $post->dir,0777,true);
        $this->_helper->viewRenderer->setNoRender(true);
    }

    /**
     *  Xoá thư mục
     *
     */
    public function removedirAction()
    {
        $post = $this->getRequest()->getPost();
        rmdir(UPLOAD_PATH ."/". $post->dir,0777,true);
        $this->_helper->viewRenderer->setNoRender(true);
    }

    /**
     * translator
     */
    public function translateAction()
    {
        $tran = Zend_Registry::get('Zend_Translate');
        if(!$this->_request->isXmlHttpRequest()){
            throw new Zend_Controller_Action_Exception($tran ->translate('_404'), 404);
        };
        $post = $this->getRequest()->getPost();
        if(empty($post["data"])){exit();}
        $text_tr = $tran ->translate($post["data"]);
        echo (Zend_Json_Encoder::encode(array("msg" => $text_tr)));
        $this->_helper->viewRenderer->setNoRender(true);
        exit();
    }
    /**
     *  Tải thông tin menu người dùng.
     */
    public  function  loadmenuAction(){
        $this->_helper->viewRenderer->setNoRender(true);
        exit();
    }

}
